//
//  ZYPictureView.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/6/30.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit

//cell之间的间距
private let itemMargin: CGFloat = 5
//计算cell的宽高
private let cellWH = (SCREENW - StatusMargin*2 - itemMargin*2) / 3

//cell的标识
private let pictViewCellId = "pictViewCellId"

class ZYPictureView: UICollectionView {
    
    //630---声明一个配图数组供外界赋值使用
    var pic_urls:[ZYPictureInfo]? {
        didSet {
                
            //print(pic_urls?.count)
            //设置countlabel的文字
            countLabel.text = "\(pic_urls?.count ?? 0)"
            
            let size = getPictureViewSize(pic_urls?.count ?? 0)
            //设置约束  
            //不要重复额设置约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
                
            }
            
            reloadData()
            
        }
        
    }
    
    //通过多少张配图计算配图的size
    func getPictureViewSize(count:Int) -> CGSize {
    
        //计算行数和列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 :count)
        let row = count == 4 ? 2: (count - 1) / 3 + 1
        
        //计算宽和高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: w, height: h)
        
    
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建视图
    //MARK: - 设置视图
    private func setupUI() {
    
        backgroundColor = UIColor.clearColor()
        
        //注册视图
        registerClass(ZYPictureViewCell.self, forCellWithReuseIdentifier: pictViewCellId)
        
        //配图里面显示的是cell,设置数据源  630====426
        dataSource = self
        
        //设置flowLayout
        let layout = collectionViewLayout as!UICollectionViewFlowLayout
        //设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        //设置垂直间距和水平间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        //添加控件
        addSubview(countLabel)
        
        //设置约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        
    }
    
    //懒加载控价
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.blueColor(), textFont: 35)
    
}


extension ZYPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictViewCellId, forIndexPath: indexPath) as! ZYPictureViewCell
        
        //cell.backgroundColor = RandomColor()
        //给cell进行赋值
        cell.pictureInfo = pic_urls![indexPath.item]
        
        return cell
    }

}

//进行自定义cell
class ZYPictureViewCell : UICollectionViewCell {
    
    //供外界赋值
    var pictureInfo:ZYPictureInfo? {
        didSet {
            //设置图片
            backImageView.sd_setImageWithURL(NSURL(string: pictureInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            
        }
    
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        //添加到控件
        addSubview(backImageView)
        
        //设置约束
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            
        }
    
    }
    
    //懒加载控件
    private lazy var backImageView: UIImageView = {
        
        let img = UIImageView(imagName: "timeline_card_top_background")
        //图片的填充方式
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        
        return img
    
    }()

}












